from sqlalchemy.orm import Session

from schemas.session import SessionResponse
from config.auth import create_token, create_payload
from middleware.exceptions import AppException


def create(db: Session, username: str, password: str) -> SessionResponse:
    # 1. 校验账户是否存在
    if username != "admin":
        raise AppException(f"该账户{username}不存在")

    # 2. 校验密码是否正确
    if password != "19940822Cxx":
        raise AppException("密码错误")

    return SessionResponse(
        access_token=create_token(create_payload(username)),
        token_type="Bearer"
    )
